module.exports = (app) => {
  const mail = require("../plugins/mail")
  const hmac = require("../plugins/hmac")
  const User = require("../models/User")
  const Code = require("../models/Code")
  const router = require("express").Router({
    // 合并父子路由路径参数
    mergeParams: true,
  })
  //资源路径
  app.use(
    "/admin/api/user",
    async (req, res, next) => {
      next()
    },
    router
  )

  //登录状态验证
  router.get("/isLogin", (req, res) => {
    if (req.session.login == true) {
      res.json({ status: 0, msg: "已登录" })
    } else res.json({ status: -1, msg: "未登录" })
  })

  //登录
  router.post("/login", async (req, res) => {
    let { email, password } = req.body
    password = await hmac(password)
    let user = await User.findOne({ email })
    if (!user) {
      res.json({ status: -1, msg: "邮箱未注册" })
    } else if (password !== user.password) {
      res.json({ status: -2, msg: "密码错误" })
    } else {
      //登陆成功后将相关信息存入session中
      req.session.login = true
      res.send(user)
    }
  })
  //logout
  router.get("/logout", (req, res) => {
    req.session.destroy()
    res.json({ status: 0, msg: "已退出" })
  })

  //code 验证
  router.post("/password", async (req, res) => {
    let { code, email, password } = req.body
    let user = await User.findOne({ email })
    if (user) {
      let myCode = await Code.findOne({ email })
      code = code + ""
      if (code !== myCode.code)
        return res.json({ status: -3, msg: "验证码错误" })
      if (myCode.ctime + 5 * 60 * 1000 > Date.now()) {
        password = hmac(password)
        await User.findOneAndUpdate({ email }, { password })
        res.json({ status: 0, msg: "密码修改成功，请重新登录" })
        req.session.destroy()
      } else {
        res.json({ status: -3, msg: "验证码已失效" })
      }
    } else res.json({ status: -3, msg: "邮箱未注册" })
  })

  //功能路由
  router.post("/sendEmail", async (req, res) => {
    let { email } = req.body
    let user = await User.findOne({ email })
    if (!user) return res.json({ status: -3, msg: "邮箱未注册" })
    let code = await Code.findOne({ email })
    //随机数
    let randNum
    if (!code) {
      randNum = parseInt(Math.random() * 899999 + "") + 100000
      await mail.sendMail(email, randNum)
      await Code.insertMany({
        email,
        ctime: Date.now(),
        code: randNum,
      })
      return res.json({ status: 0, msg: "发送成功" })
    } else if (code.ctime + 5 * 60 * 1000 < Date.now()) {
      randNum = parseInt(Math.random() * 899999 + "") + 100000
      await Code.findOneAndUpdate(
        { email },
        {
          ctime: Date.now(),
          code: randNum,
        }
      )
      await mail.sendMail(email, randNum)
      res.json({ status: 0, msg: "发送成功" })
    } else {
      res.json({ status: -1, msg: "验证码未过期,请勿重复获取" })
    }
  })

  //关注
  router.post("/follow", async (req, res) => {
    let { _id, user } = req.body
    console.log(req.body)
    let myUser = await User.findById(_id, "attentionList")
    let targetUser = await User.findById(user, "fansCount")
    let index = myUser.attentionList.indexOf(user)
    //关注状态判断
    if (index === -1) {
      myUser.attentionList.push(user)
      targetUser.fansCount++
      res.send(myUser)
    } else {
      myUser.attentionList.splice(index, 1)
      targetUser.fansCount--
      res.send(myUser)
    }
    await myUser.save()
    await targetUser.save()
  })

}
